package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.security.auth.DestroyFailedException;
import java.util.List;

/**
 * 地址簿相关接口
 */
@RestController
@Api(tags = "C端-地址簿相关接口")
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService ;

    /**
     * 根据用户id查询全部地址簿信息
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据用户id查询全部地址簿信息")
    public Result<List<AddressBook>> list() {
        log.info("根据用户id查询全部地址簿信息..." );
        List<AddressBook> addressBookList = addressBookService.list();
        return Result.success(addressBookList) ;
    }

    /**
     * 新增地址簿地址
     * @return
     */
    @ApiOperation("新增地址簿地址")
    @PostMapping
    public Result save(@RequestBody AddressBook addressBook) {
        log.info("新增地址簿地址：{}" , addressBook);
        addressBookService.save(addressBook) ;
        return Result.success() ;
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查询默认地址")
    public Result<AddressBook> getDefaultAddress() {
        log.info("查询默认地址...");
        AddressBook addressBook = addressBookService.getDefaultAddress() ;
        return Result.success(addressBook) ;
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result setDefaultAddress(@RequestBody AddressBook addressBook) {
        log.info("设置默认地址：{}" , addressBook);
        addressBookService.setDefaultAddress(addressBook) ;
        return Result.success() ;
    }

    /**
     * 根据id删除地址
     * @param id
     * @return
     */
    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result deleteById(Long id) throws DestroyFailedException {
        log.info("根据id删除地址：{}" , id);
        addressBookService.deleteById(id) ;
        return Result.success() ;
    }

    /**
     * 根据id查询地址
     * @return
     */
    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getById(@PathVariable Long id) {
        log.info("根据id查询地址：{}" , id);
        AddressBook addressBook = addressBookService.getById(id) ;
        return Result.success(addressBook) ;
    }


    /**
     * 根据id修改地址信息
     * @param addressBook
     * @return
     */
    @ApiOperation("根据id修改地址信息")
    @PutMapping
    public Result update(@RequestBody AddressBook addressBook) {
        log.info("根据id修改地址信息：{}" , addressBook);
        addressBookService.update(addressBook) ;
        return Result.success() ;
    }

}
